package com.huixue.cn.ztej.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import org.springframework.transaction.annotation.Transactional;

import com.huixue.cn.ztej.bean.EmployApply;
import com.huixue.cn.ztej.bean.EmployHour;
import com.huixue.cn.ztej.bean.Picture;
import com.huixue.cn.ztej.bean.Project;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.dao.EmployApplyDao;
import com.huixue.cn.ztej.dao.EmployHourDao;
import com.huixue.cn.ztej.dao.PictureDao;
import com.huixue.cn.ztej.dao.SysProjectMapper;
import com.huixue.cn.ztej.dao.UserMapper;
import com.huixue.cn.ztej.enums.RoleStatus;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

@Service
public class EmployApplyServiceImpl {
	
	@Autowired
	private EmployApplyDao employApplyDao;
	
	@Autowired
	private EmployHourDao employHourDao;
	
	@Autowired
	private PictureDao pictureDao;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private SysProjectMapper sysProjectMapper;
	
	public Results searchByPage(Map<String,Object> map,Integer pageNumber, Integer pageSize) {
		
		PageParameter page = new PageParameter(pageNumber,pageSize);
		map.put("page", page);

		List<EmployApply> list = employApplyDao.selectList(map);

		Map<String, Object> rmap = new HashMap<String, Object>();
		rmap.put("total", page.getTotalPage());
		rmap.put("page", pageNumber);						//当前页
		rmap.put("records", page.getTotalCount());			//总记录数
		rmap.put("pageSize", pageSize);						//每页个数
		rmap.put("rows", list);
		
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(),API_RETURN_STATUS.NORMAL.desc(),rmap);
		return results;
	}
	
	public Results searchToExamine(Map<String,Object> map,Integer pageNumber, Integer pageSize) {
		PageParameter page = new PageParameter(pageNumber-1,pageSize);
		map.put("page", page);
		List<EmployApply> list = employApplyDao.selectToExamine(map);
		
		List<EmployApply> list1 = new ArrayList<EmployApply>();
		
		
		if(list!=null && list.size()>0){
			
			Map<String,Object>  searchParams = new HashMap<>();
			for(EmployApply emp:list){
				searchParams.clear();
				emp.setApplyUser(userMapper.findById(emp.getApplyUserId()));
				list1.add(emp);
				searchParams.put("projectID", emp.getProId()+"");
				/*if(emp.getState()==0){
					searchParams.put("roleId", RoleStatus.ROLE_4.getRoleId()+"");
					List<User> userList = userMapper.findUserByParams(searchParams);
					if(userList!=null && userList.size()>0){
						emp.setNextUser(userList.get(0));
					}
				}else*/if(emp.getState()==0){
					
					searchParams.put("segmentID", emp.getSegmentId()+"");
					searchParams.put("roleId", RoleStatus.ROLE_4.getRoleId()+"");
					List<User> userList = userMapper.findUserByParams(searchParams);
					if(userList!=null && userList.size()>0){
						emp.setNextUser(userList.get(0));
					}
				}else if(emp.getState()==4){
					searchParams.put("roleId", RoleStatus.ROLE_5.getRoleId()+"");
					
					List<User> userList = userMapper.findUserByParams(searchParams);
					if(userList!=null && userList.size()>0){
						emp.setNextUser(userList.get(0));
					}
				}else if(emp.getState()==5){
					searchParams.put("roleId", RoleStatus.ROLE_6.getRoleId()+"");
					List<User> userList = userMapper.findUserByParams(searchParams);
					if(userList!=null && userList.size()>0){
						emp.setNextUser(userList.get(0));
					}
				}else{
					emp.setNextUser(new User());
				}
				
				
			}
		}
		
		long count = employApplyDao.selectCount(map);
		
		Map<String, Object> rmap = new HashMap<String, Object>();
		rmap.put("total", (count%pageSize==0)?(count/pageSize):(count/pageSize+1));
		rmap.put("page", pageNumber);						//当前页
		rmap.put("records",count);			//总记录数
		rmap.put("pageSize", pageSize);						//每页个数
		rmap.put("rows", list1);
		
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(),API_RETURN_STATUS.NORMAL.desc(),rmap);
		return results;
	}
	
	public EmployApply selectById(String id){
		return employApplyDao.selectById(id);
	}
	
	public List<EmployApply> selectList(Map<String, Object> map){
		return employApplyDao.selectList(map);
	}
	
	public List<EmployApply> selectToExamine(Map<String, Object> map){
		return employApplyDao.selectToExamine(map);
	}
	
	public Long selectCount(Map<String, Object> map){
		return employApplyDao.selectCount(map);
	}

	@Transactional(readOnly = false)
	public int insert(EmployApply employApply){
		EmployApply last = employApplyDao.findLastResult();
		Project project = sysProjectMapper.findByProId(employApply.getProId());
		
		if(last!=null && last.getVisaCode()!=null && !"".equals(last.getVisaCode())){
			String visaCode = last.getVisaCode();
			String []tmp = visaCode.split("-");
			int SerialNo = Integer.valueOf(tmp[tmp.length-1])+1;
			String newVisaCode = project.getProCode()+"-"+employApply.getConstructionName()+"-"+SerialNo;
			employApply.setVisaCode(newVisaCode);
		}else{
			String newVisaCode = project.getProCode()+"-"+employApply.getConstructionName()+"-"+1;
			employApply.setVisaCode(newVisaCode);
		}
		int applyId = employApplyDao.insert(employApply);
		EmployApply newLast = employApplyDao.findLastResult();
		if(employApply.getList()!=null){
			for(EmployHour employHour:employApply.getList()){
				employHour.setNumberId(newLast.getApplyId());
				employHourDao.insert(employHour);
			}
		}
		
		//插入图片
		if(employApply.getPicList()!=null && employApply.getPicList().size()>0){
			for(Picture picture : employApply.getPicList()){
				picture.setApplyId(newLast.getApplyId());
			}
			pictureDao.insert(employApply.getPicList());
		}
		
		return applyId;
	}
	
	public void updateById(EmployApply employApply){
		employApplyDao.updateById(employApply);
	}
	
	public void updateforExam(EmployApply employApply){
		employApplyDao.updateforExam(employApply);
	}
	
	public void deleteById(String id){
		employApplyDao.deleteById(id);
	}
	
	public Results  listEmployApplyByUserId(int applyUserId){
		
		Results results = new Results(
				API_RETURN_STATUS.NORMAL.value(),
				API_RETURN_STATUS.NORMAL.desc());
		
		List<EmployApply> list = employApplyDao.listEmployApplyByUserId(applyUserId);
		results.setData(list);
		return results;
	
	}
	
	public EmployApply  findApplyById(int applyId){
		
		EmployApply employApply = employApplyDao.findApplyById(applyId);
		
		return employApply;
	
	}

}
